//
//  UIButton+Extension.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/11.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

extension UIButton {
    
    class func buttonWithTitle(title:String , textFont:CGFloat ,textColor:UIColor , image:String? , backImage:String?) -> UIButton {
        
        let btn = UIButton()
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(textColor, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(textFont)
        
        if image != nil {
        
            let image = UIImage(named:image!)
            let imageW = Int((image?.size.width)! * 0.5)
            let imageH = Int((image?.size.height)!*0.5)
            
            btn.setImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
        }
        
        
        if backImage != nil {
        
        
            let image = UIImage(named:image!)
            let imageW = Int((image?.size.width)! * 0.5)
            let imageH = Int((image?.size.height)!*0.5)
            btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
        }
        
        btn.sizeToFit()
        
        return btn

        
    }
    
    class func buttonWithImage(imageName:String?,backImage:String? ) -> UIButton {
        
        let btn = UIButton()
//        btn.setTitle(title, forState: .Normal)
//        btn.setTitleColor(textColor, forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(textFont)
        
        if imageName != nil {
            
            let image = UIImage(named:imageName ?? "")
            let imageW = Int((image?.size.width)! * 0.5)
            let imageH = Int((image?.size.height)!*0.5)
            
            btn.setImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
            
            let hightImageName = imageName! +  "_highlighted"
            
            btn.setImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Highlighted)
        }
        
        
        if backImage != nil {
            
            
            let image = UIImage(named:backImage!)
            let imageW = Int((image?.size.width)! * 0.5)
            let imageH = Int((image?.size.height)!*0.5)
            btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(imageW, topCapHeight: imageH), forState: .Normal)
        }
        
        btn.sizeToFit()
        
        return btn
        
        
    }
}
